package com.aaa.system.service.impl;

import com.aaa.entity.Menu;
import com.aaa.system.dao.MenuDao;
import com.aaa.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: aaa-his-parent
 * @description:
 * @author: 闫克起2
 * @create: 2023-12-07 12:05
 **/
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findMenusByUserId(Long userId) {
        return menuDao.selectMenusByUserId(userId);
    }

    @Override
    public List<Menu> findLeftMenu(String phone) {
        List<Menu> menus = menuDao.selectMenusByPhone(phone);
        //1. 查询所有的一级菜单
        List<Menu> first=menus.stream().filter(item->item.getParentId().equals(0L)).collect(Collectors.toList());
        //2. 查询一级菜单下的子菜单
        for(Menu menu:first){
               List<Menu> children=new ArrayList<>();
               for(Menu m:menus){
                     if(m.getParentId().equals(menu.getMenuId())){
                         children.add(m);
                     }
               }
               menu.setChildren(children);
        }
        return first;
    }
}










